﻿using Autofac;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Models;
using ZoneLine.Views.HStationViews;

namespace ZoneLine.Views.XWStationViews
{
    /// <summary>
    /// XWStationEnableView.xaml 的交互逻辑
    /// </summary>
    public partial class XWStationEnableView : Window
    {
        public XWStationEnableView()
        {
            InitializeComponent();
            this.Loaded += StationEnableView_Loaded;
        }

        private void StationEnableView_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var config = XWStationEnableConfig.GetPara();
                if (config == null)
                {
                    DbConfig.DbContext.Insert(new XWStationEnableConfig()).ExecuteAffrows();
                    config = new XWStationEnableConfig();
                }

                XWOP010S.IsChecked = config.XWOP010SStationEnable== 1;
                XWOP010L.IsChecked = config.XWOP010LStationEnable == 1;
                XWOP010R.IsChecked = config.XWOP010RStationEnable == 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载系统配置异常" + ex.Message);
                return;
            }
        }

        private void BtnEnable(object sender, RoutedEventArgs e)
        {
            var dal = ContainerServer.Container.Resolve<IModelDAL>();
            var saveRlt = dal.UpdateModel<XWStationEnableConfig>(p => true, s =>
                 new XWStationEnableConfig()
                 {
                     XWOP010SStationEnable = XWOP010S.IsChecked == true ? 1 : 0,
                     XWOP010LStationEnable = XWOP010L.IsChecked == true ? 1 : 0,
                     XWOP010RStationEnable = XWOP010R.IsChecked == true ? 1 : 0,
                 }
            );
            if (saveRlt)
            {
                XWStationEnableConfig.Reload();
                var view = new XWMainView();
                view.Show();
                App.Current.MainWindow = view;
                this.Close();
            }
        }
    }
}
